﻿'use strict';
app.controller('ManagerPackController', function ($rootScope, $scope, $modal, $localStorage, ManagerPackFactory) {    
    $scope.overview = [],
    $scope.runings = [],
    $scope.expires = [],
    $scope.title = "Manager Pack",
    $scope.messeger = "",
    $scope.popup = function (windowClass, StyleModal, size) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: StyleModal,
            controller: "ManagerPackModalInstanceCtrl",
            size: size,
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    },
    Overview(),
    //Expires(),
    Running();
    function Overview() {
        ManagerPackFactory.getOverview($localStorage.user.UserId).then(function (response) {
            $scope.overview = response.data.Result;            
        }, function (error) {
            $scope.status = 'Unable to load country data: ' + error.message;
        });
    };
    function Running() {
        ManagerPackFactory.getPackRuning($localStorage.user.UserId).then(function (response) {
            $scope.runings = response.data;            
        }, function (error) {
            $scope.status = 'Unable to load Running data: ' + error.message;
        });
    };
    function Expires() {
        ManagerPackFactory.getPackExpires($localStorage.user.UserId).then(function (response) {
            $scope.expires = response.data;
        }, function (error) {
            $scope.status = 'Unable to load Expires data: ' + error.message;
        });
    };
})

app.controller('ManagerPackModalInstanceCtrl', function ($scope, $modalInstance, $localStorage, $http, $state, $rootScope, $modal, $timeout, $stateParams, messeger, title, BitcoinFactory) {
    $scope.messeger = messeger;
    $scope.title = title;
    $scope.WalletList = [],
    $scope.wallet = {},
    $scope.selected = [],    
    $scope.data = {
        Amount: 0,
        Wallet:0
    },
    GetBitcointoUSDRate(1),
    $scope.AlertType = "warning";
    $scope.progress = false,
    $scope.popup = function (windowClass) {
        $modal.open({
            windowClass: windowClass,
            templateUrl: "WarningModal",
            controller: "ManagerPackModalInstanceCtrl",
            size: 'sm',
            resolve: {
                messeger: function () {
                    return $scope.messeger;
                },
                title: function () {
                    return $scope.title;
                }
            }
        })
    }
    choseWalletAddPack($localStorage.user.UserId);
    $scope.doAddPack = function (data) {
        $scope.progress = true;
        $http.post($rootScope.APITransactionUrl + 'buyPack', { UserId: $localStorage.user.UserId, Amount: data.Amount, WalletId: data.Wallet }).then(function successCallback(resp) {
            if (resp.data.Status == 1) {
                $scope.AlertType = "success";
                $scope.progress = false;
                $scope.messeger = resp.data.Messeger;
                $state.transitionTo($state.current, $stateParams, {
                    reload: true,
                    inherit: false,
                    notify: true
                });
                //$state.go('app.managerpack');
                $timeout(function () {
                    $modalInstance.close();//$route.reload();                   
                }, 3600);
            } else {
                //$modalInstance.close();
                $scope.progress = false;
                $scope.messeger = resp.data.Messeger;
                $scope.AlertType = "warning";
            }
        }, function errorCallback(resp) {
            $scope.AlertType = "warning";
            $scope.progress = false;
            $scope.messeger = "Contact webmaster support";
        })
    };
    $scope.ok = function () {
        $modalInstance.close();
    };
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
    function choseWalletAddPack(UserId) {
        ManagerPackFactory.choseWalletAddPack(UserId).then(function (response) {
            $scope.WalletList = response.data.Result;
        }, function (error) {
            $scope.status = 'Unable to load Wallet Add Pack data: ' + error.message;
        });
    }
    function GetBitcointoUSDRate(Bitcoin) {
        BitcoinFactory.GetBitcointoUSDRate(Bitcoin).then(function (response) {
            $scope.data.Amount = 200 / response.data;
        }, function (error) {
            $scope.status = 'Unable to load rate data: ' + error.message;
        });
    }
});